
//删除datagrid的选中行
function eDeleteSelectRows(gridId){
	if(!gridId)
		return;
	var rows = $("#" + gridId).datagrid("getSelections");
	if (!rows || rows.length == 0)
		return;
	for ( var i = 0; i < rows.length; i++) {
		var index = $("#" + gridId).datagrid("getRowIndex",rows[i]);
		 $("#" + gridId).datagrid("deleteRow",index);
	}
}

//分组合并单元格
function gridMerge(gridID, rowsData, groupColName, mergeColName) {
	//alert("处理的数据有：" + JSON2.stringify(rowsData));
	var merges = getMergesData(rowsData, groupColName);
	if(!merges || merges.length == 0)
		return;
	//alert("merges:" + JSON2.stringify(merges));
	for ( var i = 0; i < merges.length; i++) {
		$("#" + gridID).datagrid("mergeCells", {
			index : merges[i].index,
			field : mergeColName,
			rowspan : merges[i].rowspan
		});
	}
}
// 计算分组合并行的数据
function getMergesData(rowsData, groupColName) {
	if (!rowsData || rowsData.length == 0)
		return null;
	var merges = new Array();
	var rowspan = 1;
	var rowIndex = 0;
	var colValue = rowsData[0][groupColName];

	for ( var i = 1; i < rowsData.length; i++) {
		if (colValue == rowsData[i][groupColName]) {
			rowspan += 1;
		} else {
			if (rowspan > 1) {
				merges.push({
					"index" : rowIndex,
					"rowspan" : rowspan
				});
			}
			colValue = rowsData[i][groupColName];
			rowspan = 1;
			rowIndex = i;
		}
	}
	// 要再补上最后的合并行
	if (rowspan > 1) {
		merges.push({
			"index" : rowIndex,
			"rowspan" : rowspan
		});
	}
	return merges;
}

function pagerFilter(data) {
	if (typeof data.length == 'number' && typeof data.splice == 'function') { // is
		// array
		data = {
			total : data.length,
			rows : data
		};
	}
	var dg = $(this);
	var opts = dg.datagrid('options');
	var pager = dg.datagrid('getPager');
	pager.pagination({
		onSelectPage : function(pageNum, pageSize) {
			opts.pageNumber = pageNum;
			opts.pageSize = pageSize;
			pager.pagination('refresh', {
				pageNumber : pageNum,
				pageSize : pageSize
			});
			dg.datagrid('loadData', data);
		}
	});
	if (!data.originalRows) {
		data.originalRows = (data.rows);
	}
	var start = (opts.pageNumber - 1) * parseInt(opts.pageSize);
	var end = start + parseInt(opts.pageSize);
	data.rows = (data.originalRows.slice(start, end));
	return data;
};

/*
 * $.extend($.fn.datagrid.methods, { statistics : function(jq) { var opt =
 * $(jq).datagrid('options').columns; var rows = $(jq).datagrid("getRows");
 * 
 * var footer = new Array(); footer['sum'] = ""; footer['avg'] = "";
 * footer['max'] = ""; footer['min'] = "";
 * 
 * for ( var i = 0; i < opt[0].length; i++) { if (opt[0][i].sum) { footer['sum'] =
 * footer['sum'] + sum(opt[0][i].field) + ','; } if (opt[0][i].avg) {
 * footer['avg'] = footer['avg'] + avg(opt[0][i].field) + ','; } if
 * (opt[0][i].max) { footer['max'] = footer['max'] + max(opt[0][i].field) + ','; }
 * if (opt[0][i].min) { footer['min'] = footer['min'] + min(opt[0][i].field) +
 * ','; } }
 * 
 * var footerObj = new Array();
 * 
 * if (footer['sum'] != "") { var tmp = '{' + footer['sum'].substring(0,
 * footer['sum'].length - 1) + "}"; var obj = eval('(' + tmp + ')'); if
 * (obj[opt[0][0].field] == undefined) { footer['sum'] += '"' + opt[0][4].field +
 * '":"<b>总计:</b>"'; obj = eval('({' + footer['sum'] + '})'); } else {
 * obj[opt[0][4].field] = "<b>总计:</b>" + obj[opt[0][0].field]; }
 * footerObj.push(obj); }
 * 
 * if (footer['avg'] != "") { var tmp = '{' + footer['avg'].substring(0,
 * footer['avg'].length - 1) + "}"; var obj = eval('(' + tmp + ')'); if
 * (obj[opt[0][0].field] == undefined) { footer['avg'] += '"' + opt[0][0].field +
 * '":"<b>当页均值:</b>"'; obj = eval('({' + footer['avg'] + '})'); } else {
 * obj[opt[0][0].field] = "<b>当页均值:</b>" + obj[opt[0][0].field]; }
 * footerObj.push(obj); }
 * 
 * if (footer['max'] != "") { var tmp = '{' + footer['max'].substring(0,
 * footer['max'].length - 1) + "}"; var obj = eval('(' + tmp + ')');
 * 
 * if (obj[opt[0][0].field] == undefined) { footer['max'] += '"' +
 * opt[0][0].field + '":"<b>当页最大值:</b>"'; obj = eval('({' + footer['max'] +
 * '})'); } else { obj[opt[0][0].field] = "<b>当页最大值:</b>" +
 * obj[opt[0][0].field]; } footerObj.push(obj); }
 * 
 * if (footer['min'] != "") { var tmp = '{' + footer['min'].substring(0,
 * footer['min'].length - 1) + "}"; var obj = eval('(' + tmp + ')');
 * 
 * if (obj[opt[0][0].field] == undefined) { footer['min'] += '"' +
 * opt[0][0].field + '":"<b>当页最小值:</b>"'; obj = eval('({' + footer['min'] +
 * '})'); } else { obj[opt[0][0].field] = "<b>当页最小值:</b>" +
 * obj[opt[0][0].field]; } footerObj.push(obj); }
 * 
 * if (footerObj.length > 0) { $(jq).datagrid('reloadFooter', footerObj); }
 * 
 * function sum(filed) { var sumNum = 0; for ( var i = 0; i < rows.length; i++) {
 * sumNum += Number(rows[i][filed]); } return '"' + filed + '":"' +
 * sumNum.toFixed(3) + '"'; } ;
 * 
 * function avg(filed) { var sumNum = 0; for ( var i = 0; i < rows.length; i++) {
 * sumNum += Number(rows[i][filed]); } return '"' + filed + '":"' + (sumNum /
 * rows.length).toFixed(2) + '"'; }
 * 
 * function max(filed) { var max = 0; for ( var i = 0; i < rows.length; i++) {
 * if (i == 0) { max = Number(rows[i][filed]); } else { max = Math.max(max,
 * Number(rows[i][filed])); } } return '"' + filed + '":"' + max + '"'; }
 * 
 * function min(filed) { var min = 0; for ( var i = 0; i < rows.length; i++) {
 * if (i == 0) { min = Number(rows[i][filed]); } else { min = Math.min(min,
 * Number(rows[i][filed])); } } return '"' + filed + '":"' + min + '"'; } } });
 * 
 */